package com.fangshi.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.fangshi.R
import com.fangshi.api.Record
import com.fangshi.databinding.ListItemRecordBinding

class RecordAdapter : PagingDataAdapter<Record, RecordAdapter.ViewHolder>(diffCallback) {
    class ViewHolder(binding: ListItemRecordBinding) : RecyclerView.ViewHolder(binding.root) {
        private val tvCarNo = binding.tvCarNo
        private val tvEnterTime = binding.tvEnterTime
        private val tvExitTime = binding.tvExitTime
        private val tvGarage = binding.tvGarage
        private val tvDayCount = binding.tvDayCount

        fun bindTo(record: Record?) {
            tvCarNo.text = record?.carNo
            tvEnterTime.text = record?.goInTime
            tvExitTime.text = record?.goOutTime
            tvGarage.text = record?.garageId
            tvDayCount.text = record?.actualResidenceTime
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding: ListItemRecordBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.list_item_record,
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindTo(getItem(position))
    }

    companion object {
        val diffCallback = object : DiffUtil.ItemCallback<Record>() {
            override fun areItemsTheSame(oldItem: Record, newItem: Record): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(oldItem: Record, newItem: Record): Boolean {
                return oldItem == newItem
            }

        }
    }


}